<?php
/**前台公用控制器
 * @Author: 976123967@qq.com
 * @Date:   2014-09-03 22:14:15
 * @Last Modified by:   Administrator
 * @Last Modified time: 2014-12-06 11:42:36
 */
class CommonAction Extends ExtendAction
{
	// 栏目模型
	public $categoyModel;
	// 文档模型
	public $articleModel;
	// 文档视图模型
	public $articleViewModel;
	// 图片集模型
	public $picModel;
	// 栏目cid
	public $cid; 
	// 文档cid
	public $aid;

	// 初始化 
	public function _initialize()
	{
		
		$this->cid = I('get.cid',0,'intval');
		$this->aid = I('get.aid',0,'intval');
		if(!$this->cid && ACTION_NAME !='index' && ACTION_NAME!='verify' && ACTION_NAME!='map')
			$this->redirect("index/index");
		$this->categoryModel = D('Category');
		$this->articleModel = D('Article');
		$this->articleViewModel = D('ArticleView');
		$this->picModel = D('Pic');

		$theme = I('get.t',null);
		if($theme)
		{
			session('curTheme',$theme);
		}
		elseif(!isset($_SESSION['curTheme']))
		{
			session('curTheme',C('DEFAULT_THEME'));
		}
	
		C('DEFAULT_THEME',$_SESSION['curTheme']);


	}


	/**
	 * [cover 封面页]
	 * @return [type] [description]
	 */
	public function cover()
	{
		$cid = $this->cid;
		$cms= $this->base();


	
		// 关键字
    	$cms['page_keywords'] = $cms['page_keywords']?$cms['page_keywords']:C('cfg_name');
    	//$cms['page_keywords_en'] = $cms['page_keywords_en']?$cms['page_keywords_en']:C('cfg_keywords_en');
    	// 描述
    	$cms['page_description'] = $cms['page_description']?$cms['page_description']:C('cfg_description');
    	//$cms['page_description_en'] = $cms['page_description_en']?$cms['page_description_en']:C('cfg_description_en');
		$this->assign('cms',$cms);

		


		// 获取模板
		$defaultTpl = $this->categoryModel->get_field($cid,'default_tpl');
		$this->display($defaultTpl);

	}
	

	 /**
     * [lists 列表页]
     * @return [type] [description]
     */
    public function lists()
    {
    	$cid = $this->cid;
		$cms= $this->base();



		// 关键字
    	$cms['page_keywords'] = $cms['page_keywords']?$cms['page_keywords']:C('cfg_name');
    	//$cms['page_keywords_en'] = $cms['page_keywords_en']?$cms['page_keywords_en']:C('cfg_keywords_en');
    	// 描述
    	$cms['page_description'] = $cms['page_description']?$cms['page_description']:C('cfg_decription');
    	//$cms['page_description_en'] = $cms['page_description_en']?$cms['page_description_en']:C('cfg_description_en');
		$this->assign('cms',$cms);





		// 获取模板
		$listTpl = $this->categoryModel->get_field($cid,'list_tpl');
		$this->display($listTpl);

    }


   	/**
   	 * [view 详细页面]
   	 * @return [type] [description]
   	 */
    public function view()
    {
    	$cid = $this->cid;
    	$aid = $this->aid;


		
		if(!$aid)
			$aid = $this->articleModel->get_article_cid($cid);
		
		if(!$aid)
			$this->redirect('Index/index');
		
		
		$data = $this->articleViewModel->get_article_one($aid);
	
		// 没有信息
		if(!$data)
			$this->redirect('Index/index');


		
		$cms=$this->base();
		$cms = array_merge($data,$cms);
		$cms['time'] = $data['addtime'];
		$cms['addtime'] = date('Y-m-d',$data['addtime']);
		$cms['page_keywords'] = $data['keywords'];
		//$cms['page_keywords_en'] = $data['keywords_en'];
		$cms['page_description'] = $data['description'];
		//$cms['page_description_en'] = $data['description_en'];

		
		// 页面标题
		if($cms['cat_type']!=4)
		{
			$cms['page_title'] = $cms['article_title'].' > '.$cms['page_title'];
			//$cms['page_title_en'] = $cms['article_title_en'].' > '.$cms['page_title_en'];
		}
	
		// 关键字
    	$cms['page_keywords'] = $cms['page_keywords']?$cms['page_keywords']:C('cfg_keywords');
    	//$cms['page_keywords_en'] = $cms['page_keywords_en']?$cms['page_keywords_en']:C('cfg_keywords_en');
    	// 描述
    	$cms['page_description'] = $cms['page_description']?$cms['page_description']:C('cfg_description');
    	//$cms['page_description_en'] = $cms['page_description_en']?$cms['page_description_en']:C('cfg_description_en');

		$this->assign('cms',$cms);
		$this->articleModel->update_click($data['aid']);
		

		
        // 所有图集
        $pics = $this->picModel->get_all($aid);
        $this->assign('pics',$pics);

		// 获取模板
		$listTpl = $this->categoryModel->get_field($cid,'view_tpl');
		$this->display($listTpl);
    }


    /**
     * [base 栏目基本信息]
     * @return [type]      [description]
     */
    public function base()
    {
    	$cid = $this->cid;
    
		// 栏目类型
		$data['cat_type'] = $this->categoryModel->get_field($cid,'cat_type');



		// 顶级栏目cid
		$parentCids = $this->categoryModel->get_parent_category_cid($cid);
		$parentCids = array_reverse($parentCids);
		$data['topcid']= $parentCids[0];

		// 顶级栏目名称
		$data['topname']= $this->categoryModel->get_field($parentCids[0],'cname');
		//$data['topname_en']= $this->categoryModel->get_field($parentCids[0],'cname_en');
		// 面包屑导航
		$bread = $this->categoryModel->get_bread($cid);
		$data['bread']= $bread['bread'];
		//$data['bread_en']= $bread['bread_en'];
		

		// 当前访问的栏目名称
		$data['cname']= $this->categoryModel->get_field($cid,'cname');
		//$data['cname_en']= $this->categoryModel->get_field($cid,'cname_en');

		// 当前栏目的图片
		$categoryPic = $this->categoryModel->get_field($cid,'pic');
		$data['category_pic'] = $categoryPic?__ROOT__.'/'.$categoryPic:'';


		// 标题
		$pagTitle = $this->categoryModel->get_title($cid);

		$data['page_title'] = $pagTitle['title'];
		//$data['page_title_en'] = $pagTitle['title_en'];
	
		// 关键字
		$data['page_keywords'] = $this->categoryModel->get_field($cid,'keywords');
		//$data['page_keywords_en'] = $this->categoryModel->get_field($cid,'keywords_en');
		// 描述
		$data['page_description'] = $this->categoryModel->get_field($cid,'description');
		//$data['page_description_en'] = $this->categoryModel->get_field($cid,'description_en');

		
		return $data;
    }


   

}